package com.wx_shop.api.controller.api;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx_shop.api.comm.DValid;
import com.wx_shop.api.comm.ServiceCode;
import com.wx_shop.api.service.ProductService;
import com.wx_shop.api.vo.BaseReq;
import com.wx_shop.api.vo.BaseRes;
import com.wx_shop.api.vo.CategoryVo;
import com.wx_shop.api.vo.DataPageWrapper;
import com.wx_shop.api.vo.ProductPropVo;
import com.wx_shop.api.vo.ProductVo;

/**
 * 商品api list detail
 */
@Controller
@RequestMapping("api/product")
public class ProductRest {

	@Resource
	private ProductService productService;

	/**
	 * 产品列表 - 分类搜索 - 关键字搜索
	 * 
	 * @param baseReq
	 * @param bindingResult
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "fetchPageProduct", method = RequestMethod.POST)
	public BaseRes<DataPageWrapper<ProductVo>> fetchPageProduct(
			@Validated(value = { DValid.QueryParams.class }) @RequestBody BaseReq<ProductVo> baseReq, BindingResult bindingResult) {

		BaseRes<DataPageWrapper<ProductVo>> res = null;
		if (bindingResult.hasErrors()) {
			res = new BaseRes<DataPageWrapper<ProductVo>>();
			res.setServiceCode(ServiceCode.INVALID_PARAMS);
			return res;
		}

		res = productService.fetchPageProduct(baseReq);

		return res;
	}

	@ResponseBody
	@RequestMapping(value = "categoryList")
	public BaseRes<List<CategoryVo>> categoryList() {
		return productService.categoryList();
	}

	@ResponseBody
	@RequestMapping(value = "productDetail")
	public BaseRes<ProductVo> productDetail(Integer productId) {

		BaseRes<ProductVo> res = null;
		if (productId == null) {
			res = new BaseRes<ProductVo>();
			res.setServiceCode(ServiceCode.INVALID_PARAMS);
			return res;
		}

		return productService.productDetail(productId);
	}

	@ResponseBody
	@RequestMapping(value = "productPrice")
	public BaseRes<ProductPropVo> productPrice(Integer productId) {

		BaseRes<ProductPropVo> res = null;
		if (productId == null) {
			res = new BaseRes<ProductPropVo>();
			res.setServiceCode(ServiceCode.INVALID_PARAMS);
			return res;
		}

		return productService.productPrice(productId);
	}

}
